<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Article;
use App\Http\Model\Comment;
use App\Http\Model\Images;
use App\Http\Model\Notice;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Model\User;
use Illuminate\Support\Facades\Session;

class IndexController extends Controller
{
    /**
     * 后台首页
     * @return $this
     */
    public function index(){
        if(empty(Session::get('admin')['role'])){
            /*最近的会员*/
            $users=User::select('username','avatar','created_at')->where('status',1)->limit(9)->orderBy('id','asc')->get()->all();
            /*最近评论*/
            $comments=Comment::select('user.username','user.avatar','comment.comment','comment.created_at')
                ->join('user','user.id','=','uid','left')
                ->limit(4)->orderBy('comment.id','desc')->get()->all();
            /*评论总数*/
            $num['allcomments']=Comment::count();
            $num['alluser']=User::count();
            $num['allauthor']=User::where(['is_author'=>1])->count();
            $num['allimages']=Images::where(['status'=>1])->count();
            $num['allarticle']=Article::where(['status'=>1])->count();
            return view('admin.index',compact('users','comments','num'))->with('title','后台首页');
        }else{
            $articleids=$imageids=[];
            if(Session::get('admin')['role']==2 || Session::get('admin')['role']==5){
                $articleids=Article::select('id')->where('user_id',Session::get('admin')['id'])->get()->all();
            }
            if(Session::get('admin')['role']==3 || Session::get('admin')['role']==5){
                $imageids=Images::select('id')->where('user_id',Session::get('admin')['id'])->get()->all();
            }
            $idss=array_merge($articleids,$imageids);
            $ids=[];
            foreach($idss as $id){
                $ids[]=$id->id;
            }
            $comments=Comment::select('user.username','user.avatar','comment.comment','comment.created_at')
                ->join('user','user.id','=','uid','left')->whereIn('comment.cid',$ids)
                ->limit(4)->orderBy('comment.id','desc')->get()->all();
            /*获取公告*/
            $notices=Notice::where('status',1)->get()->all();
            return view('admin.index',compact('comments','notices'))->with('title','后台首页');
        }

    }
}
